package com.yonyou.brigade.third_parties.ctripBC.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.third_parties.ctripBC.model.CtripBCConfig;
import com.yonyou.brigade.third_parties.ctripBC.service.ICtripBCOrderService;
import com.yonyou.brigade.third_parties.ctripBC.service.ICtripBCPublicService;
import com.yonyou.brigade.third_parties.ctripBC.service.ICtripBCUserService;
import com.yonyou.ybz.ctrip.pojo.CtripGroupMember;
import com.yonyou.ybz.ctrip.service.ICtripGroupMemberService;
import com.yonyou.ybzboot.starter.common.thread.InvocationInfoProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Auther: kfw
 * @Date: 2019/1/4 10:32
 * @Description: 携程大客户拉单测试
 */
@Controller
@RequestMapping("/ctrip")
public class CtripBCTestController {

    @Value("${dubbo.protocol.port}")
    private String dubboPort;

    @Autowired
    @Qualifier("ctripBCOrderServiceImpl")
    private ICtripBCOrderService ctripBCOrderService;

    @Autowired
    @Qualifier("briChannelServiceImpl")
    private IBriChannelService briChannelService;

    @Autowired
    @Qualifier("ctripBCUserServiceImpl")
    private ICtripBCUserService ctripBCUserService;

    @Autowired
    private ICtripBCPublicService ctripBCPublicService;

    @Reference
    @Qualifier("ctripGroupMemberServiceImpl")
    private ICtripGroupMemberService ctripGroupMemberService;

//    @ResponseBody
//    @RequestMapping(value = "/getCtripBCOrder", method = RequestMethod.POST)
//    public String getOrders(@RequestBody Map<String, String> params) {
//        String tenant = params.get("tenant");
//        String orgPk = params.get("orgPk");
//        String begin = params.get("begin");
//        String end = params.get("end");
//        int channelId = Integer.parseInt(params.get("channelId"));
//        CtripBCConfig channelConfig = (CtripBCConfig) briChannelService.getChannelConfig(tenant, ChannelEnum.CTRIPBC.getName(), orgPk, new CtripBCConfig());
//        ctripBCOrderService.getCtripBCOrders(tenant, orgPk, begin, end, channelId, channelConfig);
//        return "success";
//    }

//    @ResponseBody
//    @RequestMapping(value = "/syncUser", method = RequestMethod.GET)
//    public void syncUser(HttpServletResponse response) throws IOException {
//        ctripBCUserService.getLoginInfo("ft9fbcrw", "0001A210000000003ADC", "15901062973", "Home", 1, response);
//    }

    @ResponseBody
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public String test(@RequestBody Map<String, String> params) {
        String tenantId = params.get("tenantId");
        String orgpk = params.get("orgpk");
        String searchType = params.get("searchType");
        String orderIds = params.get("orderIds");
        CtripBCConfig ctripBCConfig = (CtripBCConfig) briChannelService.getChannelConfig(tenantId, ChannelEnum.CTRIPBC.getName(), orgpk, new CtripBCConfig());
        String s = ctripBCPublicService.doOrdersSearch(searchType, orderIds, ctripBCConfig);
        return s;
    }

    @ResponseBody
    @RequestMapping(value = "/dubboPort", method = RequestMethod.GET)
    public String getDubboPort() {
        return dubboPort;
    }

    @ResponseBody
    @GetMapping(value = "/groupmember")
    public List<CtripGroupMember> getGroupMember(String userId, String tenantId) {
        Wrapper<CtripGroupMember> wrapper = new EntityWrapper<>();
        wrapper.eq(CtripGroupMember.USERID, userId);
        InvocationInfoProxy.setTenantid(tenantId);
        return ctripGroupMemberService.selectList(wrapper);
    }

}
